/*
 * @Author: your name
 * @Date: 2021-11-02 10:44:09
 * @LastEditTime: 2021-11-04 17:06:34
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /my-draw/src/utils/draw/myMap.ts
 */
import type {
  Feature,
  GeoJSON,
  FeatureCollection,
  Geometry,
  Point,
  Position,
  MultiPoint,
  MultiLineString,
  MultiPolygon,
  Polygon,
  LineString,
  GeoJsonProperties
} from './draw.constant';
import { ECursor } from './draw.constant'
import Style from './draw.style'

export class MyMap {
  constructor(mapbox: any) {
    this.mapbox = mapbox // mapbox
    // this.mapbox = mapbox.map // WebGIS
  }

  protected mapbox: any

  /**
   * @description: addLayer
   * @param {string} id
   * @param {GeoJSON} feature
   * @param {GeoJsonProperties} paint
   * @param {GeoJsonProperties} layout
   */
  protected addLayer = {
    line: (id: string, feature?: GeoJSON, paint?: GeoJsonProperties, layout?: GeoJsonProperties) => {
      if (this.mapbox.getLayer(id)) return
      this.mapbox.addLayer({
        id,
        source: !!feature ? { type: 'geojson', data: feature } : id,
        type: 'line',
        layout: layout || Style.LINE_LAYOUT || {},
        paint: paint || Style.LINE_PAINT || {}
      })
    },
    circle: (id: string, feature?: GeoJSON, paint?: GeoJsonProperties, layout?: GeoJsonProperties) => {
      if (this.mapbox.getLayer(id)) return
      this.mapbox.addLayer({
        id,
        source: !!feature ? { type: 'geojson', data: feature } : id,
        type: 'circle',
        paint: paint || Style.POINT_PAINT || {},
        layout: layout || {}
      })
    },
    fill: (id: string, feature?: GeoJSON, paint?: GeoJsonProperties, layout?: GeoJsonProperties) => {
      if (this.mapbox.getLayer(id)) return
      this.mapbox.addLayer({
        id,
        source: !!feature ? { type: 'geojson', data: feature } : id,
        type: 'fill',
        paint: paint || Style.FILL_PAINT || {},
        layout: layout || {}
      })
    },
  }

  /**
   * @description: addSource
   * @param {string} id
   * @param {GeoJSON} geojson
   */
  protected addSource = (id: string, geojson: GeoJSON): boolean => {
    if (!!this.mapbox.getSource(id)) return false
    this.mapbox.addSource(id, geojson)
    return true
  }

  /**
   * @description: getSource().setData()
   * @param {string} id
   * @param {GeoJSON} data
   */
  protected updateSource = (id: string, data: GeoJSON) => {
    !!this.mapbox.getSource(id) && this.mapbox.getSource(id).setData(data)
  }

  /**
   * @description: removeLayer
   * @param {string} id
   */
  protected removeLayer = (id: string): boolean => {
    return !!(!!this.mapbox.getLayer(id) && this.mapbox.removeLayer(id))
  }

  /**
   * @description: removeSource
   * @param {string} id
   */
  protected removeSource = (id: string): boolean => {
    return !!(!!this.mapbox.getSource(id) && this.mapbox.removeSource(id))
  }

  /**
   * @description: removeLayerAndSource
   * @param {string} id
   */
  protected removeLayerAndSource = (id: string): boolean => {
    return this.removeLayer(id) && this.removeSource(id)
  }

  /**
   * @description: 改变光标样式
   * @param {string | undefined} type
   */
  protected changeCursor = (type?: ECursor) => {
    this.mapbox.getCanvas().style.cursor = type || ''
  }

}